/*
 * @lc app=leetcode.cn id=793 lang=cpp
 *
 * [793] 阶乘函数后 K 个零
 */

// @lc code=start
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
    long long calTailZeroNumber(long long k)
    {
        if (k == 0)
            return 0;
        long long t = 0;
        long long ans = 0;
        while((t = k / 5) != 0)
        {
            ans += t;
            k = t;
        }
        return ans;
    }
    int preimageSizeFZF(long long k)
    {
        long long l = 4 * k, r = 5 * k + 1;
        while (l < r)
        {
            long long mid = l + (r - l) / 2;
            long long ans = calTailZeroNumber(mid);
            if(ans == k) return 5;
            if(ans < k)
            {
                l = mid + 1;
            }
            else
            {
                r = mid;
            }
        }
        return 0;
    }
};

// long long main()
// {
//     return 0;
// }

// @lc code=end
